// Copyright 2021 Guoyoko. All Rights Reserved.


#include "Graph/CustomizeFlowGraphNode.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "Graph/CustomizeFlowGraph.h"
//#include "Node/SelfNode.h"


#define LOCTEXT_NAMESPACE "CustomizeFlowGraphNode"


void UCustomizeFlowGraphNode::AllocateDefaultPins()
{
	UEdGraphPin* Inputs = CreatePin(EGPD_Input, TEXT("Transition"), TEXT("In"));
	UEdGraphPin* Outputs = CreatePin(EGPD_Output, TEXT("Transition"), TEXT("Out"));
}

void UCustomizeFlowGraphNode::AutowireNewNode(UEdGraphPin* FromPin)
{
	Super::AutowireNewNode(FromPin);

	//@TODO: If the FromPin is a state, create a transition between us
	if (FromPin)
	{
		if (GetSchema()->TryCreateConnection(FromPin, GetInputPin()))
		{
			FromPin->GetOwningNode()->NodeConnectionListChanged();
		}
	}
}

FText UCustomizeFlowGraphNode::GetNodeTitle(ENodeTitleType::Type TitleType) const
{
	//const USelfNode* TempNodeObj = Cast<USelfNode>(NodeInstance);
	//if (TempNodeObj != NULL)
	//{
	//	return FText::FromName(TempNodeObj->GetNodeName());
	//}

	return FText::FromString(TEXT("None"));
}

FText UCustomizeFlowGraphNode::GetTooltipText() const
{
	return LOCTEXT("SelfNode_Tooltip", "This is a node");
}

//void UCustomizeFlowGraphNode::NodeConnectionListChanged()
//{
//	Super::NodeConnectionListChanged();
//	GetSelfGraph()->UpdateAsset();
//}

void UCustomizeFlowGraphNode::PostPlacedNewNode()
{
	// NodeInstance can be already spawned by paste operation, don't override it

	UClass* NodeClass = ClassData.GetClass(true);
	if (NodeClass && (NodeInstance == nullptr))
	{
		UEdGraph* MyGraph = GetGraph();
		UObject* GraphOwner = MyGraph ? MyGraph->GetOuter() : nullptr;
		if (GraphOwner)
		{
			NodeInstance = NewObject<UObject>(GraphOwner, NodeClass);
			NodeInstance->SetFlags(RF_Transactional);
		}
	}
}

UEdGraphPin* UCustomizeFlowGraphNode::GetInputPin() const
{
	return Pins[0];
}

UEdGraphPin* UCustomizeFlowGraphNode::GetOutputPin() const
{
	return Pins[1];
}

//class USelfGraph* UCustomizeFlowGraphNode::GetSelfGraph()
//{
//	return CastChecked<USelfGraph>(GetGraph());
//}

#undef LOCTEXT_NAMESPACE

